package com.tusuo.stds.ui.scenedetail;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.LevelDetail;
import com.tusuo.stds.rest.SceneDetail;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.suggest.EmptyPresenter;
import com.tusuo.stds.ui.suggest.EmptyView;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(LevelDetailActivity.class)
@EActivity(R.layout.activity_level_detail)
public class LevelDetailActivity extends BaseToolbarActivity<EmptyView,EmptyPresenter> {

    @Inject
    ApiHelper mApiHelper;

    @ViewById(R.id.level_detail_layout_description)
    LinearLayout layoutDescription;
    @ViewById(R.id.level_detail_tv_description)
    TextView tvDescription;

    @ViewById(R.id.level_detail_tv_completion)
    TextView tvCompletion;

    @ViewById(R.id.level_detail_layout_pass)
    LinearLayout layoutPass;
    @ViewById(R.id.level_detail_layout_perfect_pass)
    LinearLayout layoutPerfectPass;

    @StringRes(R.string.err_msg_server)
    String mErrMsgServer;

    private SceneDetail.SceneLevel level;

    @AfterInject
    void initDI() {
        Injector.initLevelDetailActivityComponent().inject(this);

        Intent intent = getIntent();
        if (intent.getExtras() != null) {
            level = Parcels.unwrap(intent.getParcelableExtra("level"));
        }
    }

    @NonNull
    @Override
    public EmptyPresenter createPresenter() {
        return new EmptyPresenter();
    }

    @AfterViews
    void initData() {
        setupUI(this);

        String title;
        if (level != null) {
            title = level.Title;
        } else {
            title = "关卡详情";
        }

        ToolbarOwner.Config config = new ToolbarOwner.Config(title, true, R.drawable.ic_back_light, null);
        mToolbarOwner.setConfig(config);

        if (level == null) {
            return;
        }

        mApiHelper.getLevelDetail(new GetLevelDetailSubscriber(), level.Id);

        if (level.LevelType == 1) {
            layoutPass.setVisibility(View.GONE);
            layoutPerfectPass.setVisibility(View.VISIBLE);
            tvCompletion.setText(getResources().getText(R.string.level_perfect_pass_tips));
        }else if (level.LevelType == 10){
            layoutPass.setVisibility(View.VISIBLE);
            layoutPerfectPass.setVisibility(View.GONE);
            tvCompletion.setText(getResources().getText(R.string.level_pass_tips));
        }
    }

    @UiThread
    void updateLevelDescription(String description) {
        if (TextUtils.isEmpty(description)) {
            layoutDescription.setVisibility(View.GONE);
        } else {
            tvDescription.setText(description);
        }
    }

    private class GetLevelDetailSubscriber extends Subscriber<LevelDetail> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final LevelDetail levelDetail) {
            if (levelDetail.checkState(LevelDetailActivity.this)) {
               updateLevelDescription(levelDetail.Description);
            }
        }
    }
}
